package user.view;

import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.ericaro.surfaceplotter.JSurfacePanel;

import org.math.plot.PlotPanel;

import system.activity.MainListener;
import user.activity.Vizualizer;

public class VisualizerImpl extends JPanel implements Vizualizer {
	ArrayList<PicturePanelImpl> pictures = new ArrayList<PicturePanelImpl>();
	ArrayList<Integer> plotNumbers = new ArrayList<Integer>();
	ArrayList<Integer> picturesNumbers = new ArrayList<Integer>();
	ArrayList<JPanel> plots = new ArrayList<JPanel>();
	private JPanel controlsPanel;
	private JPanel menuPanel;
	private JButton zoomIn;
	private JButton zoomOut;
	private javax.swing.JPanel content;
	private javax.swing.JScrollPane scrollPanel;
	
	private JPanel plotPanel;
	private JComboBox comboBox;
	private JLabel zoomValue;

	private MainListener listener;

	public VisualizerImpl() {
		initComponents();
		repaint();
		show();
	}

	@Deprecated
	public void addPicture(PicturePanelImpl picture) {
		pictures.add(picture);
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(pictures.get(0), BorderLayout.CENTER);
		panel.setSize(pictures.get(0).getImage().getWidth(), pictures.get(0)
				.getImage().getHeight());
		scrollPanel.setViewportView(panel);// по умолчанию всегда
											// показываем первую из
											// списка
		comboBox.addItem(pictures.size());
		comboBox.setSelectedIndex(0);
		repaint();
		show();
		listener.needRepaint();
		picturesNumbers.add(comboBox.getItemCount());
	}

	public void addPicture(PicturePanelImpl picture, String name) {
		pictures.add(picture);
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(pictures.get(0), BorderLayout.CENTER);
		panel.setSize(pictures.get(0).getImage().getWidth(), pictures.get(0)
				.getImage().getHeight());
		scrollPanel.setViewportView(panel);// по умолчанию всегда
											// показываем первую из
											// списка
		comboBox.addItem(name == null ? pictures.size() : name);
		comboBox.setSelectedIndex(0);
		repaint();
		show();
		listener.needRepaint();
		picturesNumbers.add(comboBox.getItemCount());
	}
	
	public void addPanel(PlotPanel panel, String name)
	{
		System.out.println("ADD PLOT");
		panel.setSize(this.getSize());
		plots.add(panel);
		comboBox.addItem(name == null? comboBox.countComponents()+1: name);
		plotNumbers.add(comboBox.getItemCount());
		comboBox.setSelectedIndex(0);
	}

	@Override
	public void setListener(MainListener listener) {
		this.listener = listener;
	}

	private void initComponents() {
		controlsPanel = new javax.swing.JPanel();
		comboBox = new JComboBox();
		comboBox.setSize(400, 25);
		zoomValue = new JLabel();
		zoomValue.setBounds(0, 0, 150, 35);
		zoomValue.setText("100%");
		Font font = new Font("sansserif", Font.BOLD, 32);
		zoomValue.setFont(font);
		zoomIn = new javax.swing.JButton();
		zoomOut = new javax.swing.JButton();
		menuPanel = new javax.swing.JPanel();
		scrollPanel = new javax.swing.JScrollPane();
		content = new javax.swing.JPanel();
		menuPanel.add(comboBox);
		controlsPanel.add(zoomValue);
		plotPanel = new JPanel();
		plotPanel.setSize(600, 600);
		controlsPanel.setName("controlsPanel"); // NOI18N

		zoomIn.setText("+"); // NOI18N
		zoomIn.setName("zoomIn"); // NOI18N

		zoomOut.setText("-"); // NOI18N
		zoomOut.setName("zoomOut"); // NOI18N

		javax.swing.GroupLayout controlsPanelLayout = new javax.swing.GroupLayout(
				controlsPanel);
		controlsPanel.setLayout(controlsPanelLayout);
		controlsPanelLayout
				.setHorizontalGroup(controlsPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								controlsPanelLayout
										.createSequentialGroup()
										.addContainerGap(281, Short.MAX_VALUE)
										.addComponent(
												zoomIn,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												73,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												zoomOut,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												73,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(11, 11, 11)));
		controlsPanelLayout
				.setVerticalGroup(controlsPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								controlsPanelLayout
										.createSequentialGroup()
										.addGap(26, 26, 26)
										.addGroup(
												controlsPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(zoomIn)
														.addComponent(zoomOut))
										.addContainerGap(19, Short.MAX_VALUE)));

		ActionListener comboBoxAction = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {

				if (plotNumbers.contains(Integer.valueOf(comboBox.getSelectedIndex() + 1))) {
					plotPanel.setVisible(true);
					scrollPanel.setVisible(false);
					controlsPanel.setVisible(false);
					plotPanel.removeAll();
					plotPanel.setSize(1000, 1000);
					JPanel p = plots.get(plotNumbers.indexOf(Integer.valueOf(comboBox.getSelectedIndex() + 1)));

					plotPanel.setMinimumSize(new java.awt.Dimension(150, 150));
					plotPanel.setName("plot"); // NOI18N

					javax.swing.GroupLayout contentLayout = new javax.swing.GroupLayout(
							plotPanel);
					plotPanel.setLayout(contentLayout);
					contentLayout.setHorizontalGroup(contentLayout.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 446,
							Short.MAX_VALUE));
					contentLayout.setVerticalGroup(contentLayout.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 448,
							Short.MAX_VALUE));
					
					
					plotPanel.add(p);
					show();
					listener.needRepaint();
				} else if (picturesNumbers.contains(Integer.valueOf(comboBox
						.getSelectedIndex() + 1))) {
					plotPanel.setVisible(false);
					scrollPanel.setVisible(true);
					controlsPanel.setVisible(true);
					scrollPanel.setViewportView(pictures.get(picturesNumbers
							.indexOf(comboBox.getSelectedIndex() + 1)));
					zoomValue.setText(""
							+ ((int) (pictures.get(
									picturesNumbers.indexOf(comboBox
											.getSelectedIndex() + 1))
									.getZoomValue() * 100)) + "%");
					show();
					listener.needRepaint();
				}
			}
		};
		comboBox.addActionListener(comboBoxAction);

		menuPanel.setName("menuPanel"); // NOI18N

		javax.swing.GroupLayout menuPanelLayout = new javax.swing.GroupLayout(
				menuPanel);
		menuPanel.setLayout(menuPanelLayout);
		menuPanelLayout.setHorizontalGroup(menuPanelLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 438,
				Short.MAX_VALUE));
		menuPanelLayout.setVerticalGroup(menuPanelLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 39,
				Short.MAX_VALUE));

		scrollPanel.setName("scrollPanel"); // NOI18N

		content.setAutoscrolls(true);
		content.setMinimumSize(new java.awt.Dimension(150, 150));
		content.setName("content"); // NOI18N

		javax.swing.GroupLayout contentLayout = new javax.swing.GroupLayout(
				content);
		content.setLayout(contentLayout);
		contentLayout.setHorizontalGroup(contentLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 446,
				Short.MAX_VALUE));
		contentLayout.setVerticalGroup(contentLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 448,
				Short.MAX_VALUE));

		scrollPanel.setViewportView(content);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														controlsPanel,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						scrollPanel,
																						javax.swing.GroupLayout.Alignment.LEADING,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						438,
																						Short.MAX_VALUE)
																				.addComponent(
																						plotPanel,
																						javax.swing.GroupLayout.Alignment.LEADING,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						1000,
																						Short.MAX_VALUE)
																				.addComponent(
																						menuPanel,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE))
																.addContainerGap()))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(menuPanel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(scrollPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(plotPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(controlsPanel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)));

		AdjustmentListener scrollListener = new AdjustmentListener() {

			@Override
			public void adjustmentValueChanged(AdjustmentEvent arg0) {
				Adjustable adj = arg0.getAdjustable();
				try {
					if (adj.getOrientation() == Adjustable.HORIZONTAL) {
						if (pictures.size() != 0) {
							pictures.get(
									picturesNumbers.indexOf(comboBox
											.getSelectedIndex() + 1)).setScrollX(adj.getValue());
						}
					} else {
						if (pictures.size() != 0) {
							pictures.get(
									picturesNumbers.indexOf(comboBox
											.getSelectedIndex() + 1)).setScrollY(adj.getValue());
						}
					}
				} catch (Exception e) {
				}
			}
		};

		scrollPanel.getVerticalScrollBar()
				.addAdjustmentListener(scrollListener);
		scrollPanel.getHorizontalScrollBar().addAdjustmentListener(
				scrollListener);
		zoomIn.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				zoomValue.setText(""
						+ ((int) (pictures.get(
								picturesNumbers.indexOf(comboBox
										.getSelectedIndex() + 1))
								.zoomIn() * 100)) + "%");
				zoomValue.repaint();
				zoomValue.show();
				listener.needRepaint();
			}
		});

		zoomOut.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				zoomValue.setText(""
						+ ((int) (pictures.get(
								picturesNumbers.indexOf(comboBox
										.getSelectedIndex() + 1))
								.zoomOut() * 100)) + "%");
				zoomValue.repaint();
				zoomValue.show();
				listener.needRepaint();
			}
		});
	}

	public void clearImages() {
		/*
		 * System.out.println("CLEAR"); pictures = new
		 * ArrayList<PicturePanelImpl>(); comboBox.removeAllItems();
		 * scrollPanel.removeAll();
		 * System.out.println(""+comboBox.getItemCount() +"  "+
		 * pictures.size()); repaint(); show(); listener.needRepaint();
		 */

	}

	public void addPanel(JSurfacePanel panel, String name) {
		System.out.println("ADD PLOT 2");
		panel.setSize(this.getSize());
		plots.add(panel);
		comboBox.addItem(name == null? comboBox.countComponents()+1: name);
		plotNumbers.add(comboBox.getItemCount());
		comboBox.setSelectedIndex(0);
	}

}